﻿using System.Collections.Generic;

namespace AAA.Command.WCF
{
    public abstract class DefaultCommand : ICommand
    {
        protected static readonly int NG = -1;
        protected static readonly int OK = 0;

        #region ICommand Members
        public virtual int BeforeExecute(Dictionary<string, string> model)
        {
            return OK;
        }

        protected abstract int ExecuteCommand(Dictionary<string, string> model);

        public virtual void ReleaseResource()
        {
            
        }

        public int Execute(Dictionary<string, string> model)
        {
            int iRC;

            if ((iRC = BeforeExecute(model)) != OK)
            {
                ReleaseResource();
                return iRC;
            }

            if ((iRC = ExecuteCommand(model)) != OK)
            {
                ReleaseResource();
                return iRC;
            }

            if ((iRC = AfterExecute(model)) != OK)
            {
                ReleaseResource();
                return iRC;
            }
            
            ReleaseResource();
            return iRC;
        }

        public virtual int AfterExecute(Dictionary<string, string> model)
        {
            return OK;
        }

        #endregion
    }
}
